package com.csu.tingtian.controller;


import com.csu.tingtian.common.CommonResponse;
import com.csu.tingtian.common.ResponseCode;
import com.csu.tingtian.entity.Account;
import com.csu.tingtian.entity.LegalFarm;
import com.csu.tingtian.entity.UnDeterminedFarms;
import com.csu.tingtian.entity.UnLegalFarm;
import com.csu.tingtian.service.AccountService;
import com.csu.tingtian.service.AdminService;
import com.csu.tingtian.service.ProduceCommentService;
import com.csu.tingtian.vo.FarmAndLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private ProduceCommentService produceCommentService;


    //登录，无需访问数据库
    @GetMapping("login/{username}")
    public CommonResponse<String[]> getAdmin(@PathVariable("username") String userName) {
        if(userName.equals("admin")) {
            String[] roles = new String[1];
            roles[0] = "super";
            return CommonResponse.createForRoles(roles);
        }
        else
            return CommonResponse.createForError(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"非管理员");
    }

    //获取所有申请成为商家的信息
    @GetMapping("undeterminedfarms")
    @ResponseBody
    public CommonResponse<List<UnDeterminedFarms>> getAllUnDeterminedFarms() {
        return adminService.getAllUnDeterminedFarms();
    }

    //插入一个商家到legalfarm
    @PostMapping("legalfarms")
    @ResponseBody
    public CommonResponse<LegalFarm> insertLegalFarm(@RequestBody UnDeterminedFarms unDeterminedFarms) {
        return adminService.insertLegalFarm(unDeterminedFarms);
    }


    //插入一个不合格的商家到unlegalfarm
    @PostMapping("unlegalfarms/{decription}")
    @ResponseBody
    public CommonResponse<UnLegalFarm> insertUnLegalFarm(@RequestBody UnDeterminedFarms unDeterminedFarms,@PathVariable("decription") String decription) {
        return adminService.insertUnLegalFarm(unDeterminedFarms,decription);
    }

    @GetMapping("farmname")
    CommonResponse<List<FarmAndLevel>> returnFarmNameStarIsTwoOrLower(){
        return adminService.getLevelUnder();
    }
}
